package edu.westga.cs3202.sokoban;

import java.awt.*;
import java.util.Random;

import javax.swing.*;

/**
 * GamePanel creates the components for the board and draws it.
 * 
 * @author Lewis Williams
 * @version 2011.2.17
 */
public class GamePanel extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	//private Image ball;
	private Image player;
	
	private Image vwall, hwall;
	private Image tlcorner, trcorner, blcorner, brcorner;
	private Image lend, rend, bend, tend;
	private Image tright, tleft, tup, tdown, tcross;
	private Image redghost, blueghost, pinkghost, orangeghost;

	private GameBoard board;
	private Controller gameController;
	private int cellHeight, cellWidth;
	//private final Color WALK_TILE = new Color(84,84,84);
	//private final Color WALL_TILE = new Color(116,28,8);
	private final Color GOAL_TILE = new Color(248,214,2);
	
    public GamePanel(GameBoard board) {
    	this.setPreferredSize(new Dimension(board.getBoardColumns() * 20, board.getBoardRows() * 20));
    	this.setBackground(Color.BLACK);
    	this.board = board;
    	this.gameController = new Controller(board, this);
    	
    	//this.ball = Toolkit.getDefaultToolkit().getImage("images/ball.gif");
    	this.player = Toolkit.getDefaultToolkit().getImage("images/pacmanleft.gif");
    	
    	this.vwall = Toolkit.getDefaultToolkit().getImage("images/vwall.png");
    	this.hwall = Toolkit.getDefaultToolkit().getImage("images/hwall.png");
    	
    	this.tlcorner = Toolkit.getDefaultToolkit().getImage("images/tlcorner.png");
    	this.trcorner = Toolkit.getDefaultToolkit().getImage("images/trcorner.png");
    	this.blcorner = Toolkit.getDefaultToolkit().getImage("images/blcorner.png");
    	this.brcorner = Toolkit.getDefaultToolkit().getImage("images/brcorner.png");
    	
    	this.lend = Toolkit.getDefaultToolkit().getImage("images/lend.png");
    	this.rend = Toolkit.getDefaultToolkit().getImage("images/rend.png");
    	this.bend = Toolkit.getDefaultToolkit().getImage("images/bend.png");
    	this.tend = Toolkit.getDefaultToolkit().getImage("images/tend.png");
    	
    	this.tleft = Toolkit.getDefaultToolkit().getImage("images/tleft.png");
    	this.tright = Toolkit.getDefaultToolkit().getImage("images/tright.png");
    	this.tup = Toolkit.getDefaultToolkit().getImage("images/tup.png");
    	this.tdown = Toolkit.getDefaultToolkit().getImage("images/tdown.png");
    	this.tcross = Toolkit.getDefaultToolkit().getImage("images/tcross.png");
    	
    	this.redghost = Toolkit.getDefaultToolkit().getImage("images/redghost.png");
    	this.blueghost = Toolkit.getDefaultToolkit().getImage("images/blueghost.png");
    	this.pinkghost = Toolkit.getDefaultToolkit().getImage("images/pinkghost.png");
    	this.orangeghost = Toolkit.getDefaultToolkit().getImage("images/orangeghost.png");
    	
    	GUIObserver observer = new GUIObserver(gameController);
    	this.addKeyListener(observer);
		this.setFocusable(true);
    }
    
    public void refresh(){
    	repaint();
    }
    
    public void setPlayerGIF(String filepath){
    	this.player = Toolkit.getDefaultToolkit().getImage(filepath);
    }
    
    public void paintComponent(Graphics g) {
    	super.paintComponent(g);
		
		// Get the entire board and paint it
    	for(int rows=0; rows < board.getBoardRows(); rows++){   		
    		for(int columns=0; columns < board.getBoardColumns(); columns++){    			
    			char current = board.getCharacter(rows, columns);
    			//Draws a player
    			if(current == 'p') {   								
    				g.drawImage(player, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a vertical wall
    			else if(current == '|'){
    				g.drawImage(vwall, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a horizontal wall
    			else if(current == '_'){
    				g.drawImage(hwall, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a top left corner
    			else if(current == '1'){
    				g.drawImage(tlcorner, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a top right corner
    			else if(current == '2'){
    				g.drawImage(trcorner, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a bottom left corner
    			else if(current == '3'){
    				g.drawImage(blcorner, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a bottom right corner
    			else if(current == '4'){
    				g.drawImage(brcorner, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a left end piece
    			else if(current == '<'){
    				g.drawImage(lend, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}//Draws a right end piece
    			else if(current == '>'){
    				g.drawImage(rend, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a top end piece
    			else if(current == '^'){
    				g.drawImage(tend, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a bottom end piece
    			else if(current == 'v'){
    				g.drawImage(bend, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a right facing t
    			else if(current == '5'){
    				g.drawImage(tleft, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a right facing t
    			else if(current == '6'){
    				g.drawImage(tright, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a right facing t
    			else if(current == '7'){
    				g.drawImage(tup, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a right facing t
    			else if(current == '9'){
    				g.drawImage(tdown, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a right facing t
    			else if(current == '+'){
    				g.drawImage(tcross, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    			}
    			//Draws a goal square
    			else if(current == 'g'){
    				g.setColor(GOAL_TILE);
    				g.fillRect(columns*cellWidth, rows*cellHeight, cellWidth, cellHeight);
    			}
    			//Draws a random colored ghost
    			else if(current == 'b'){
    				Random ghostSelector = new Random();
    				int ghost = ghostSelector.nextInt(4);
    				if(ghost == 0){
    					g.drawImage(redghost, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    				}
    				else if(ghost == 1){
    					g.drawImage(blueghost, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    				}
    				else if(ghost == 2){
    					g.drawImage(pinkghost, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    				}
    				else if(ghost == 3){
    					g.drawImage(orangeghost, columns*cellWidth, rows*cellHeight, cellHeight, cellWidth, this);
    				}
    				
    			}
    			//Draws a black walking tile
    			else if(current == ' '){
    				g.setColor(Color.BLACK);
    				g.fillRect(columns*cellWidth, rows*cellHeight, cellWidth, cellHeight);
    			}
    		}
    	}
//    	cellWidth = this.getWidth()/20;
//    	cellHeight = this.getHeight()/20;
    	cellWidth = 20;
    	cellHeight = 20;
    }    
}